package com.web.article;

import com.bean.Article;
import com.service.ArticleService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddArticleServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Article article=new Article();
        ArticleService service=new ArticleService();

        try {

            BeanUtils.populate(article,req.getParameterMap());
            service.add(article);

           article.setPubtime(service.getPubTime(article.getTitle()));
           req.getRequestDispatcher("/ListArticle").forward(req,resp);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
